import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:yp/config/style.dart';
import 'package:yp/Xwidgets/button_widget.dart';
import 'package:yp/Xwidgets/index.dart';

import '../helper.dart';

typedef ClickCallback = void Function(int selectIndex, String selectString,
    {Map<String, dynamic> info});

class YpShowDialog {
  static void dialog(BuildContext context, Widget content) {
    showDialog(
        context: context,
        // 点击空白收
        // barrierDismissible: false,
        builder: (BuildContext context) {
          return Padding(
            padding: const EdgeInsets.symmetric(horizontal: 25),
            child: Material(
                type: MaterialType.transparency,
                child: Center(
                  child: content,
                )),
          );
        });
  }

  ///  自提
  static void showPickUpStation(
      BuildContext context, List info, ClickCallback clickCallback) {
    int selectIndex;
    for (var i = 0; i < info.length; i++) {
      if (info[i]['isSelect']) {
        selectIndex = i;
      }
    }

    showDialog(
        context: context,
        // 点击空白收
        // barrierDismissible: false,
        builder: (BuildContext context) {
          return StatefulBuilder(builder: (context1, state1) {
            return Padding(
              padding: const EdgeInsets.symmetric(horizontal: 25),
              child: Material(
                  type: MaterialType.transparency,
                  child: Center(
                    child: IntrinsicHeight(
                      child: Container(
                        decoration: BoxDecoration(
                            color: EsColors.White,
                            borderRadius: BorderRadius.circular(10)),
                        child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: <Widget>[
                              SizedBox(height: 18),
                              Text('选择自提点',
                                  style: TextStyle(
                                      fontSize: EsSizes.SM,
                                      color: EsColors.Content,
                                      fontWeight: EsFontWeights.Normal),
                                  textAlign: TextAlign.center),
                              Container(
                                height:
                                    (info.length > 4 ? 4 : info.length) * 60.0,
                                child: ListView.separated(
                                    shrinkWrap: true,
                                    padding: EdgeInsets.zero,
                                    itemBuilder: (context, index) {
                                      return GestureDetector(
                                        behavior: HitTestBehavior.opaque,
                                        onTap: () {
                                          state1(() {
                                            print('切换自选地点');

                                            for (var i = 0;
                                                i < info.length;
                                                i++) {
                                              if (i == index) {
                                                info[i]['isSelect'] = true;
                                                selectIndex = index;
                                              } else {
                                                info[i]['isSelect'] = false;
                                              }
                                            }
                                          });
                                        },
                                        child: Padding(
                                          padding: const EdgeInsets.symmetric(
                                              horizontal: 12, vertical: 20),
                                          child: Row(
                                            crossAxisAlignment:
                                                CrossAxisAlignment.center,
                                            children: [
                                              Image.asset(
                                                  info[index]['isSelect']
                                                      ? 'assets/images/selectIcon1.png'
                                                      : 'assets/images/select1.png',
                                                  width: 18,
                                                  height: 18),
                                              SizedBox(width: 8),
                                              Expanded(
                                                child: Column(
                                                  crossAxisAlignment:
                                                      CrossAxisAlignment.start,
                                                  children: [
                                                    Visibility(
                                                      visible: info[index]
                                                                  ['name'] !=
                                                              null &&
                                                          info[index]['name'] !=
                                                              '',
                                                      child: Padding(
                                                        padding:
                                                            const EdgeInsets
                                                                    .only(
                                                                bottom: 8),
                                                        child: Text(
                                                          info[index]['name'],
                                                          style: TextStyle(
                                                              color: EsColors
                                                                  .Title,
                                                              fontSize:
                                                                  EsSizes.SM,
                                                              fontWeight:
                                                                  EsFontWeights
                                                                      .Medium),
                                                        ),
                                                      ),
                                                    ),
                                                    Text(
                                                      '上海市静安区万航路889号越大广航芜湖起飞B1-902室',
                                                      style: TextStyle(
                                                          color:
                                                              EsColors.Content,
                                                          fontSize: EsSizes.SM,
                                                          fontWeight:
                                                              EsFontWeights
                                                                  .Normal),
                                                    ),
                                                  ],
                                                ),
                                              )
                                            ],
                                          ),
                                        ),
                                      );
                                    },
                                    separatorBuilder: (context, index) =>
                                        YpLine(),
                                    itemCount: info.length),
                              ),
                              EsBottomWidget(
                                  leftTitle: null,
                                  rightTitle: '确定',
                                  rightlick: () {
                                    if (selectIndex != null) {
                                      print('确定');
                                      Navigator.of(context).pop();
                                      clickCallback(selectIndex,
                                          info[selectIndex]['name'],
                                          info: {});
                                    } else {
                                      toast('请选择自提点');
                                    }
                                  },
                                  decoration: null)
                            ]),
                      ),
                    ),
                  )),
            );
          });
        });
  }

  /// 完善会员奖励
  static void showCompleteMemberReward(BuildContext context, Map info,
      {ClickCallback clickCallback}) {
    YpShowDialog.dialog(
        context,
        Container(
          width: double.infinity,
          height: 235,
          margin: const EdgeInsets.symmetric(horizontal: 20),
          padding: const EdgeInsets.symmetric(horizontal: 30, vertical: 25),
          alignment: Alignment.center,
          decoration: EsDecoration.BoxShadowWithRadius,
          child: Center(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  '恭喜您获得以下奖励',
                  style: TextStyle(
                      fontSize: EsSizes.XL,
                      color: EsColors.Primary,
                      fontWeight: EsFontWeights.Medium),
                ),
                SizedBox(height: 6),
                RichText(
                  text: TextSpan(children: [
                    _buildCenteredTextSpan(
                        text: '积分',
                        width: 50,
                        style: TextStyle(
                            fontSize: EsSizes.SM,
                            color: EsColors.Title,
                            fontWeight: EsFontWeights.Normal)),
                    _buildCenteredTextSpan(
                        text: '+${info['integral_point']!=null?info['integral_point'].toString():''}',
                        width: 50,
                        style: TextStyle(
                            fontSize: EsSizes.SM,
                            color: EsColors.Pink,
                            fontWeight: EsFontWeights.Normal)),
                  ]),
                ),
                RichText(
                  text: TextSpan(children: [
                    _buildCenteredTextSpan(
                        text: '成长值',
                        width: 50,
                        style: TextStyle(
                            fontSize: EsSizes.SM,
                            color: EsColors.Title,
                            fontWeight: EsFontWeights.Normal)),
                    _buildCenteredTextSpan(
                        text: '+${info['growth_point']!=null?info['growth_point'].toString():''}',
                        width: 50,
                        style: TextStyle(
                            fontSize: EsSizes.SM,
                            color: EsColors.Pink,
                            fontWeight: EsFontWeights.Normal)),
                  ]),
                ),
                RichText(
                  text: TextSpan(children: [
                    _buildCenteredTextSpan(
                        text: '优惠券',
                        width: 50,
                        style: TextStyle(
                            fontSize: EsSizes.SM,
                            color: EsColors.Title,
                            fontWeight: EsFontWeights.Normal)),
                    _buildCenteredTextSpan(
                        text: 'x${info['coupon_quantity']!=null?info['coupon_quantity'].toString():''}张',
                        width: 50,
                        style: TextStyle(
                            fontSize: EsSizes.SM,
                            color: EsColors.Pink,
                            fontWeight: EsFontWeights.Normal)),
                  ]),
                ),
                Expanded(child: SizedBox(height: 20)),
                Padding(
                  padding: const EdgeInsets.only(top: 0, bottom: 18),
                  child: RichText(
                    textAlign: TextAlign.center,
                    text: TextSpan(
                        text: '奖品详情请于',
                        style: TextStyle(
                            fontSize: EsSizes.SM,
                            color: EsColors.Title,
                            fontWeight: EsFontWeights.Normal),
                        children: <TextSpan>[
                          TextSpan(
                              text: '我的奖励',
                              style: TextStyle(
                                  fontSize: EsSizes.SM,
                                  color: EsColors.Primary,
                                  fontWeight: EsFontWeights.Normal),
                              recognizer: TapGestureRecognizer()
                                ..onTap = () {
                                  print('我的奖励');
                                  Navigator.of(context).pop();
                                }),
                          TextSpan(
                              text: '中查看',
                              style: TextStyle(
                                  fontSize: EsSizes.SM,
                                  color: EsColors.Title,
                                  fontWeight: EsFontWeights.Normal)),
                        ]),
                  ),
                ),
                Center(
                  child: EsOutLineButton(
                    '好的',
                    padding: const EdgeInsets.symmetric(
                        vertical: 10, horizontal: 45),
                    bgColor: EsColors.Primary,
                    textColor: EsColors.White,
                    onTap: () {
                      Navigator.of(context).pop();
                    },
                  ),
                ),
              ],
            ),
          ),
        ));
  }

  /// 获取抽奖机会
  static void showAchieveChance(BuildContext context,
      {List info, ClickCallback clickCallback}) {
    YpShowDialog.dialog(
        context,
        Container(
          width: double.infinity,
          height: 235,
          padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 25),
          alignment: Alignment.center,
          decoration: EsDecoration.BoxShadowWithRadius,
          child: Center(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Text(
                  '获取抽奖机会',
                  style: TextStyle(
                      fontSize: EsSizes.XL,
                      color: EsColors.Title,
                      fontWeight: EsFontWeights.Medium),
                ),
                Expanded(child: SizedBox()),
                _achieveChanceItem('还可以使用积分兑换3次', '兑换', () {
                  print('兑换');
                  Navigator.of(context).pop();
                }),
                Expanded(child: SizedBox()),
                _achieveChanceItem('还可以分享获取一次', '分享', () {
                  print('分享');
                  Navigator.of(context).pop();
                }),
                Expanded(child: SizedBox()),
                _achieveChanceItem('还可以下单获取1次', '去下单', () {
                  print('去下单');
                  Navigator.of(context).pop();
                }),
                Expanded(child: SizedBox()),
              ],
            ),
          ),
        ));
  }

  /// 抱歉没中奖
  static void showSorry(BuildContext context) {
    YpShowDialog.dialog(
        context,
        Container(
          width: double.infinity,
          height: 250,
          padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 25),
          alignment: Alignment.center,
          decoration: EsDecoration.BoxShadowWithRadius,
          child: Center(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Text(
                  '抱歉，本次没有抽中哦',
                  style: TextStyle(
                      fontSize: EsSizes.XL,
                      color: EsColors.Title,
                      fontWeight: EsFontWeights.Medium),
                ),
                Expanded(child: SizedBox()),
                Text(
                  '😭',
                  style: TextStyle(
                      fontSize: 70,
                      color: EsColors.Title,
                      fontWeight: EsFontWeights.Medium),
                ),
                Expanded(child: SizedBox()),
                GestureDetector(
                  onTap: () {
                    Navigator.of(context).pop();
                  },
                  child: Container(
                      width: double.infinity,
                      height: 44,
                      margin: EdgeInsets.symmetric(horizontal: 20),
                      alignment: Alignment.center,
                      decoration: BoxDecoration(
                        gradient: LinearGradient(
                          begin: Alignment.topCenter,
                          end: Alignment.bottomCenter,
                          colors: [Color(0xfffde8a3), Color(0xffffac35)],
                        ),
                        borderRadius: BorderRadius.all(Radius.circular(55)),
                      ),
                      child: Text(
                        '再试试吧',
                        style: TextStyle(
                            fontSize: EsSizes.LG,
                            color: Color(0xff9a0201),
                            fontWeight: EsFontWeights.Medium),
                      )),
                ),
                Expanded(child: SizedBox()),
              ],
            ),
          ),
        ));
  }
}

/// 获取抽奖机会的item
Widget _achieveChanceItem(String title, String btnTitle, Function onTap) {
  return Row(
    mainAxisAlignment: MainAxisAlignment.spaceBetween,
    children: [
      Text(
        title ?? '',
        style: TextStyle(
            fontSize: EsSizes.SM,
            color: EsColors.Title,
            fontWeight: EsFontWeights.Normal),
      ),
      GestureDetector(
        onTap: onTap,
        child: Container(
            width: 80,
            height: 32,
            alignment: Alignment.center,
            decoration: BoxDecoration(
              gradient: LinearGradient(
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
                colors: [Color(0xfffde8a3), Color(0xffffac35)],
              ),
              borderRadius: BorderRadius.all(Radius.circular(55)),
            ),
            child: Text(
              btnTitle ?? '',
              style: TextStyle(
                  fontSize: EsSizes.SM,
                  color: Color(0xff9a0201),
                  fontWeight: EsFontWeights.Medium),
            )),
      )
    ],
  );
}

WidgetSpan _buildCenteredTextSpan(
    {String text, double width, TextStyle style}) {
  return WidgetSpan(
    alignment: PlaceholderAlignment.bottom,
    child: SizedBox(width: 50, child: Text(text, style: style)),
  );
}
